package com.yamaha.bikemarket;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

import com.yamaha.bikemarket.util.CommonUtilities;
import com.yamaha.bikemarket.util.LogUtil;
import com.yamaha.bikemarket.util.Utils;

public class AActivity extends Activity {

	private boolean isRegisterd = false;
	private final Object syncObject = new Object();

	private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String newMessage = intent.getExtras().getString(
					CommonUtilities.EXTRA_MESSAGE);
			// Waking up mobile if it is sleeping
			WakeLocker.acquire(getApplicationContext());

			LogUtil.v("AActivity", "Display Notification : " + newMessage);
			Toast.makeText(getApplicationContext(), newMessage,
					Toast.LENGTH_LONG).show();

			/**
			 * Take appropriate action on this message depending upon your app
			 * requirement For now i am just displaying it on the screen
			 * */

			// Releasing wake lock
			WakeLocker.release();
		}
	};

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LogUtil.v("Activity", this.getClass().getSimpleName()
				+ " Enter Create Mode.");

		synchronized (syncObject) {
			if (!isRegisterd) {
				registerReceiver(mHandleMessageReceiver, new IntentFilter(
						CommonUtilities.DISPLAY_MESSAGE_ACTION));
				isRegisterd = true;
			}
		}
	}

	@Override
	protected void onRestart() {

		LogUtil.v("Activity", this.getClass().getSimpleName()
				+ " Enter onRestart Mode.");

		super.onRestart();
	}

	@Override
	protected void onDestroy() {

		LogUtil.v("Activity", this.getClass().getSimpleName()
				+ " Enter Destroy Mode.");

		synchronized (syncObject) {
			if (isRegisterd) {
				unregisterReceiver(mHandleMessageReceiver);
				isRegisterd = false;
			}
		}

		Utils.closeAllDialog();

		super.onDestroy();
	}

	@Override
	protected void onStart() {

		LogUtil.v("Activity", this.getClass().getSimpleName()
				+ " Enter Start Mode.");

		super.onStart();
	}

	@Override
	protected void onPause() {

		LogUtil.v("Activity", this.getClass().getSimpleName()
				+ " Enter Pause Mode.");

		synchronized (syncObject) {
			if (isRegisterd) {
				unregisterReceiver(mHandleMessageReceiver);
				isRegisterd = false;
			}
		}

		Utils.closeAllDialog();

		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();

		LogUtil.v("Activity", this.getClass().getSimpleName()
				+ " Enter Resume Mode.");

		synchronized (syncObject) {
			registerReceiver(mHandleMessageReceiver, new IntentFilter(
					CommonUtilities.DISPLAY_MESSAGE_ACTION));
			isRegisterd = true;
		}
	}
}
